/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ThreadClient;

import Packets.pkValue;
import StaticClass.ArrSocket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;
import javax.swing.JOptionPane;
//import sun.java2d.pipe.ValidatePipe;

/**
 *
 * @author Oho
 */
public class ThreadFile extends Thread {

    Socket sk = null;
    int port;
    private ObjectInputStream clientData;

    public ThreadFile(int port) {
        this.port = port;
    }

    public ThreadFile(Socket sk) {
        this.sk = sk;
    }
    ObjectInputStream inp = null;
    ObjectOutputStream outp = null;

    public void ThreadWrite(String text) throws IOException {
        File f = new File(text);
        FileInputStream fis = new FileInputStream(f);
        BufferedInputStream bis = new BufferedInputStream(fis);
//
        DataInputStream dis = new DataInputStream(bis);

        if(outp ==null)
        {
            

             outp = new ObjectOutputStream( sk.getOutputStream());
        }
        //Sending size of file.
        // DataOutputStream dos = new DataOutputStream(os);
//        dos.writeUTF(clientSocket+);
        outp.writeUTF(f.getName());
        
        outp.flush();
        outp.writeLong(f.length());
        outp.flush();
        byte[] arr = new byte[1024];
        try {
            int len = 0;
            while ((len = dis.read(arr)) != -1) {
                outp.write(arr, 0, len);

            }
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex);

        }
        System.out.println("da nhan du lieu thanh cong!!!");
        outp.flush();
        fis.close();
        bis.close();
        dis.close();
       

    }

    public void ThreadRead1() throws FileNotFoundException, IOException, IOException, ClassNotFoundException {
        if(clientData ==null)
            clientData = new ObjectInputStream(sk.getInputStream());
        pkValue pk = (pkValue) clientData.readObject();
        if (pk.getObj().toString().equals("GUIFILE") == true) {



            String name = clientData.readUTF();
            long size = clientData.readLong();
            
            int bytesRead;
            FileOutputStream output = new FileOutputStream(name);
            byte[] buffer = new byte[1024];
            while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
                output.write(buffer, 0, bytesRead);
                size -= bytesRead;
            }
            output.flush();
            output.close();
         //   clientData.close();
//            byte[] mybytearray = (byte[]) vl.getObj();
//            bos.write(mybytearray, 0, mybytearray.length);
//            bos.flush();
//            long end = System.currentTimeMillis();
            // System.out.println(end-start);
            System.out.println("da goi xong");
        } else {
            String str[] = (String[]) pk.getObj();
            for (int i = 0; i < str.length; i++) {
                ThreadWrite(str[i]);
            }
        }
//        clientData.close();

    }

    public void ThreadRead() throws FileNotFoundException, IOException, IOException, ClassNotFoundException {
        if (inp == null) {
            inp = new ObjectInputStream(sk.getInputStream());
        }
        pkValue name = (pkValue) inp.readObject();



        pkValue vl = (pkValue) inp.readObject();
        int filesize = 6022386; // filesize temporary hardcoded
        long start = System.currentTimeMillis();
        int bytesRead;
        int current = 0;
        FileOutputStream fos = new FileOutputStream(name.getObj().toString());
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        byte[] mybytearray = (byte[]) vl.getObj();
        bos.write(mybytearray, 0, mybytearray.length);
        bos.flush();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        System.out.println("da goi xong");

    }

    @Override
    public void run() {


        while (true) {
            try {
                try {
                    //while (true) {
                    //
                    //

                    ThreadRead1();
                    // inp.close();
                    //} catch (FileNotFoundException ex) {
                    //  Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
                    ///} catch (IOException ex) {
                    //
                    //}



                    //}
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }
}
